package com.hua.fanqiemall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hua.fanqiemall.product.entity.BrandEntity;
import com.hua.fanqiemall.product.vo.BrandRspVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hua.fanqiemall.product.entity.CategoryBrandRelationEntity;
import com.hua.fanqiemall.product.service.CategoryBrandRelationService;
import com.hua.common.utils.PageUtils;
import com.hua.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author luoweihua
 * @email lwh_559@163.com
 * @date 2021-12-09 22:14:00
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 获取分类关联的品牌
     * @author luoweihua
     * @date 2022/1/12 11:35
     * @param catId
     * @return R
     *
     * 1、Controller:处理请求，接收和校验数据
     * 2、Service:接收Controller传来的数据，进行业务处理
     * 3、Controller接收Service处理完的数据后，封装页面指定的VO
     */
    @GetMapping("/brands/list")
    public R relationBrandsList(@RequestParam(value = "catId",required = false) Long catId){
       List<BrandEntity> brandEntityList = categoryBrandRelationService.getBrandsByCatId(catId);
       List<BrandRspVo> brandRspVoList = new ArrayList<>();
        for (BrandEntity brandEntity : brandEntityList) {
            BrandRspVo brandRspVo = new BrandRspVo();
            brandRspVo.setBrandId(brandEntity.getBrandId());
            brandRspVo.setBrandName(brandEntity.getName());
            brandRspVoList.add(brandRspVo);
        }
        return R.ok().put("data",brandRspVoList);
    }

    /**
     * 品牌分类关联查询列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        return R.ok().put("data", data);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
